package com.zdsoft.blp.zongbuju.shouye.bianmin.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;

import com.zdsoft.blp.R;
import com.zdsoft.blp.gongyong.API.API;
import com.zdsoft.blp.gongyong.net.BaseActivity;
import com.zdsoft.blp.gongyong.net.Net;
import com.zdsoft.blp.gongyong.net.NetCallback;
import com.zdsoft.blp.gongyong.xlistview.XListView;
import com.zdsoft.blp.zongbuju.shouye.bianmin.adapter.BMbmdhAdapter;
import com.zdsoft.blp.zongbuju.shouye.bianmin.entity.Dianhua;
import com.zdsoft.blp.zongbuju.shouye.linliquan.entity.LinLi;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/8/30.
 */
public class BMbmdhActivity extends BaseActivity implements XListView.IXListViewListener,NetCallback{
    private TextView tv_title;
    private XListView xlv_bmdh;
    private List<Dianhua> list;
    private List<Dianhua> mlist;
    private Dianhua dianhua;
    private BMbmdhAdapter adapter;
    private int pageNo=1;
    private String id;
    @Override
    protected void init() {
        setContentView(R.layout.bianmin_bmdh);
        xlv_bmdh=find(R.id.xlv_bmdh);
        tv_title = find(R.id.tv_title);
        tv_title.setText("便民电话");
        xlv_bmdh.setXListViewListener(this);
        xlv_bmdh.setPullRefreshEnable(true);//设置可以刷新
        xlv_bmdh.setPullLoadEnable(true);//设置可以加载更多
        mlist=new ArrayList<>();
        Intent intent=getIntent();
         id=intent.getStringExtra("id");
        showMoreData();
        xlv_bmdh.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Uri uri =Uri.parse("tel:"+ mlist.get(position-1).getPhone().toString().trim());
                Intent phoneIntent = new Intent(Intent.ACTION_DIAL,uri);
                startActivity(phoneIntent);
            }
        });
    }

    private void showMoreData() {

        Net net=new Net();
        net.url= API.BMINFO_URL;
        net.nameSpace=API.BMINFO_NAMESPACE;
        net.methodName=API.BMINFOLIST ;
        SharedPreferences sp = getSharedPreferences("info", Context.MODE_PRIVATE);
        String communityCode = sp.getString("communityCode", "00001");
        Map<String,Object> map=new HashMap<>();
        map.put("pageNo",pageNo);
        map.put("pageSize",7);
        map.put("columnId",id);
        map.put("communityCode",communityCode);
        map.put("condition","");
        map.put("curTime","");
        net.parms=map;
        getServer(net,this);
    }
    @Override
    public void onRefresh() {
        mlist=new ArrayList<>();
        pageNo=1;
        showMoreData();
    }

    @Override
    public void onLoadMore() {
       pageNo++;
        showMoreData();
    }

    @Override
    public void onResponse(boolean flag, int code, String res) {
    Log.e("res",res);
        if (flag) {
            if (pageNo == 1) {
                mlist = jiexi(res);
                adapter = new BMbmdhAdapter(this, mlist);
                xlv_bmdh.setAdapter(adapter);
                xlv_bmdh.stopRefresh();
            } else {
                list = jiexi(res);
                for (Dianhua dianhuas : list) {
                    mlist.add(dianhuas);
                }
                adapter.setListData(mlist);
                xlv_bmdh.stopLoadMore();
            }
        }
    }

    private List<Dianhua> jiexi(String res) {
       list=new ArrayList<>();
        try {
            JSONObject object=new JSONObject(res);
            JSONArray array=object.getJSONArray("rows");
            for(int i=0;i<array.length();i++){
                JSONObject object2=array.getJSONObject(i);
                String contactPhone=object2.getString("contactPhone");
                String title=object2.getString("title");
                dianhua=new Dianhua(title,contactPhone);
                list.add(dianhua);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return list;
    }
}
